package com.guyaning.deyson_core.core.delegates.bottom;

import android.view.KeyEvent;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.guyaning.deyson_core.core.R;
import com.guyaning.deyson_core.core.delegates.DeysonDelegate;

/**
 * Created by Administrator on 2018/5/25.
 */

public abstract class BottomItemDelegate extends DeysonDelegate implements View.OnKeyListener {

    private long mExitTime = 0;

    private static final int EXIT_TIME = 2000;

    @Override
    public void onResume() {

        super.onResume();

        final  View rootView = getView();

        if(rootView!=null){

            rootView.setFocusableInTouchMode(true);

            rootView.requestFocus();

            rootView.setOnKeyListener(this);

        }
    }

    @Override
    public boolean onKey(View view, int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - mExitTime) > EXIT_TIME) {
                ToastUtils.showShort("双击退出" + getString(R.string.app_name));
                mExitTime = System.currentTimeMillis();
            } else {
                _mActivity.finish();

                if (mExitTime != 0) {
                    mExitTime = 0;
                }
            }
            return true;
        }
        return false;
    }
}
